package leetcode

//https://leetcode.com/problems/longest-palindrome

/**
 * 直接统计 出现成对的字母次数，然后乘以2，出现单独的字母 +1
 *
 */
class LongestPalindrome {

    fun longestPalindrome(s: String): Int {
        var len = 0
        val hashSet = hashSetOf<Char>()
        for (i in 0 until s.length) {
            if (hashSet.contains(s[i])) {
                len += 2
                hashSet.remove(s[i])
            } else hashSet.add(s[i])
        }

        //最后如果set为空说明是成对出现，反之有单个字母+1即可
        return if (hashSet.isEmpty()) len else len + 1
    }
}